package bramar.all;

import java.sql.*;
import java.io.*;
import java.util.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DatabaseMetaData;
import java.sql.Statement;
 

 
 


public class AccessToOracleTable   

{

    
     
	public AccessToOracleTable(String table_name)throws SQLException, ClassNotFoundException 
	{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection acon=DriverManager.getConnection("jdbc:odbc:accdsn");
		Connection ocon=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
		Statement st=acon.createStatement();
		Statement st1=ocon.createStatement();		
		StringBuffer result = new StringBuffer();
		 
		try
		{
			StringBuffer sql=new StringBuffer();
			sql.append("select * from ");
			sql.append(table_name);
			ResultSet rs=st.executeQuery(sql.toString());
			ResultSetMetaData rsmd=rs.getMetaData();
			result.append("create table ");
			result.append(table_name);
			result.append("(");
			for(int i=1;i<=rsmd.getColumnCount();++i)
			{
				if(i!=1)
					result.append(',');
				result.append(rsmd.getColumnName(i));
				result.append(' ');
				String type=processType(rsmd.getColumnTypeName(i),rsmd.getPrecision(i));
				result.append(type);

				if (rsmd.getPrecision(i) < 65535)
					{
						result.append('(');
						result.append(rsmd.getPrecision(i));
						/*if (rsmd.getScale(i) > 0)
						 {
							result.append(',');
							result.append(rsmd.getScale(i));
						 }*/
						result.append(") ");
					} else
					result.append(' ');
				
				/*if (rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls)
					result.append("NOT NULL ");
				else
					result.append("NULL ");
				if (rsmd.isAutoIncrement(i))
					result.append(" auto_increment");*/
			}

      /*DatabaseMetaData dbmd = acon.getMetaData();
      ResultSet primary = dbmd.getPrimaryKeys(null, null, table_name);

      boolean first = true;
      while (primary.next())
      {
        if (first)
        {
          first = false;
          result.append(',');
          result.append("primary key(");
        } else
          result.append(",");
 
        result.append(primary.getString("COLUMN_NAME"));
      }

      if (!first)
        result.append(')');*/

      result.append(")");
System.out.println("Query is"+result.toString());
st1.executeUpdate(result.toString());
 
    } catch (SQLException e)
    {
      e.printStackTrace();
    }
        }
        

                
 /* pivate static String processType(String type1 , int size)
  {
    String usigned = "UNSIGNED";
    int i = type1.indexOf(usigned);
    if( i!=-1 )
      type1 = type1.substring(0,i)+type1.substring(i+usigned.length());
    
    if( type1.equalsIgnoreCase("varchar") && (size==65535))
      type1 = "TEXT";
    
    return type1.trim();
  }*/                        






   private static String processType(String columnTypeName, int precision)
    {
      //throw new UnsupportedOperationException("Not yet implemented");
      
        
    if( columnTypeName.equalsIgnoreCase("number"))
      columnTypeName = "NUMBER";
    if( columnTypeName.equalsIgnoreCase("double"))
      columnTypeName = "NUMBER";
    
    if( columnTypeName.equalsIgnoreCase("text"))
      columnTypeName = "VARCHAR2";
    
 
    return columnTypeName.trim();
    }
}


